package org.lanqiao.tjut.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.lanqiao.tjut.bean.AdminBean;
import org.lanqiao.tjut.bean.OperatorBean;
import org.lanqiao.tjut.bean.UserInfoBean;
import org.lanqiao.tjut.service.IOperatorInfoService;
import org.lanqiao.tjut.service.IUserInfoService;
import org.lanqiao.tjut.service.IUserLoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Kui
 *
 */
@Controller
public class UserLoginAction {
	// 注入Service
	@Resource
	private IUserLoginService adminS;
	@Resource
	private IOperatorInfoService opS;
	@Resource
	private IUserInfoService userS;

	@RequestMapping("/regist")
	public String regist(AdminBean adminB) {
		// 输出管理员注册信息
		System.out.println("用户注册：");
		System.out.println("用户注册姓名：" + adminB.getAdmins_name());
		System.out.println("用户注册密码:" + adminB.getAdmins_psw());
		// 添加管理员信息
		adminS.addAdmin(adminB);
		return "../../login";

	}
	@RequestMapping("/login")
	public String login(HttpServletRequest request, AdminBean adminB, OperatorBean opB,UserInfoBean userB) {
		// 获取管理员姓名
		String strName = request.getParameter("textName");
		// 获取管理员密码
		String strPsw = request.getParameter("textPsw");
		// 获取用户登录类型
		String strUser = request.getParameter("user");
		// 管理员类型
		if (strUser.equals("1")) {
			// 设置管理员姓名
			adminB.setAdmins_name(strName);
			adminB.setAdmins_psw(strPsw);
			// 获取用户登录类型
			List<AdminBean> lstAdminS = adminS.findAdminByNameAndPwd(adminB);
			// 登录成功
			// 将登陆成功的用户信息，缓存到session中
			request.getSession().setAttribute("LoginedAdminInfo", lstAdminS.get(0));
			if (lstAdminS != null && lstAdminS.size() > 0) {
				return "logined";
			} else {
				return "../../login";
			}

		} else if (strUser.equals("2")) {

			// 设置管理员姓名
			opB.setOpName(strName);
			opB.setOpPsw(strPsw);
			// 获取用户登录类型
			List<OperatorBean> lstOps = opS.findOperByNameAndPwd(opB);
			// 登录成功
			// 将登陆成功的用户信息，缓存到session中
			request.getSession().setAttribute("LoginedOperatorInfo", lstOps.get(0));
			if (lstOps != null && lstOps.size() > 0) {
				return "operatorlogined";
			} else {
				return "../../login";
			}
		}else if(strUser.equals("3")){


			// 设置管理员姓名
			userB.setUserName(strName);
			userB.setUserPsw(strPsw);
			// 获取用户登录类型
			List<UserInfoBean> lstUserS = userS.findUserByNameAndPwd(userB);
			// 登录成功
			// 将登陆成功的用户信息，缓存到session中
			request.getSession().setAttribute("LoginedUserInfo", lstUserS.get(0));
			if (lstUserS != null && lstUserS.size() > 0) {
				return "userlogined";
			} else {
				return "../../login";
			}
		
			
		}
		return "../../login";
	}

}
